package com.kinyx.framework.enums;

import java.util.Locale;
import java.util.Map;

import com.kinyx.framework.annotation.assist.CanNull;
import com.kinyx.framework.annotation.assist.ReturnNotNull;
import com.kinyx.framework.annotation.assist.Tester;
import com.kinyx.framework.annotation.assist.Throws;
import com.kinyx.framework.exception.IErrorCode;
import com.kinyx.framework.utils.KStringUtils;

/**
 * 错误编码枚举定义接口
 */
@Tester("com.kinyx.framework.enums.IErrorCodeEnum_Tester")
public interface IErrorCodeEnum extends IErrorCode, I18NEnum {

	/** 号段数量 */
	int SECTION_COUNT = 1000;

	/** @return 枚举序号 */
	int ordinal();

	/** @return 错误编码号段 */
	int section();

	// 实现类不应修改
	@Override
	default int getErrorCode() {
		final int section = this.section();
		return (section * SECTION_COUNT) + (section > 0 ? this.ordinal() : -this.ordinal());
	}

	/** @return 错误信息（英文内容） */
	@ReturnNotNull
	default String getErrorMessage() {
		return KStringUtils.hump2words(this.toString(), " ", false);
	}

	// 实现类不应修改
	@Override
	default String getCategory() { return "errorCode-" + this.section(); }

	// 实现类不应修改
	@Override
	@ReturnNotNull
	default String getValue() {
		return this.getErrorMessage();
	}

	// 实现类不应修改
	@Override
	@Throws(at = "KReader.read(InputStream, String)", value = "UnsupportedEncodingException", when = "new InputStreamReader(InputStream, String)")
	@Throws(at = "KReader.read(InputStream, String)", value = "IOException", when = "InputStreamReader.read()")
	//	@Throws(at = "KFileReader.openStream(String)", value = "ErrorCode0.FileDoesNotExists", when = "ClassLoader.getSystemResource(String) == null")
	@Throws(at = "KFileReader.openStream(String)", value = "IOException", when = "URL.openStream()")
	@Throws(at = "KPropertiesReader.read(String, String)", value = "IOException", when = "Properties.load(Reader)")
	@Throws(at = "KI18NUtils.getStringValue(Locale, String, String)", value = "ErrorCodeX.NotConfiged", when = "未配置文件或未配置值")
	default String getErrorMessage(@CanNull(defaultValue = "Configs.default_locale") final Locale locale, @CanNull final Map<String, Object> params) {
		return KStringUtils.replaceEL(this.getValue(locale), params);
	}

}
